//
//  TJNewFeatureViewController.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/12.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class TJNewFeatureViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 注册cell.
        self.collectionView!.registerClass(TJCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }
    
    let flowlayout = UICollectionViewFlowLayout()
    
    init (){
    
        super.init(collectionViewLayout: flowlayout)
        flowlayout.itemSize = self.view.bounds.size
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.minimumLineSpacing = 0
        flowlayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return NumberOfNewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TJCollectionViewCell

       cell.index = indexPath.item
   
        return cell
    }
}

// MARK 自定义cell(同一个文件定义多个类)
class TJCollectionViewCell:UICollectionViewCell {


    // 当前cell显示的页码
    var index: Int = 0 {
        didSet {
            // 当控制器给cell的index设置值后
            // 拼接图片名称
            let imageName = "new_feature_\(index + 1)"
//            TJPrint(imageName)
            newImgView.image = UIImage(named: imageName)
            // 当页面发生变化时候,隐藏按钮.
            startBtn.hidden = true
        }
    }
    
    // 给cell添加子控件.
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        // 1,添加子控件.
        contentView.addSubview(newImgView)
        contentView.addSubview(startBtn)
        
        // 2,添加约束.
        newImgView.translatesAutoresizingMaskIntoConstraints = false
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        newImgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    // MARK - "开始体验"按钮点击事件.
    @objc private func StartBtnClick() {
    
//        TJPrint("开始按钮点击,请切换窗口更控制器")
        (UIApplication.sharedApplication().delegate as! AppDelegate).changeWindowRootVC(TJTabBarController())
        
    }
    
    // MARK - 懒加载.
    // 图片.
    lazy var newImgView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    // 按钮.
    lazy var startBtn:UIButton = {
    
        let btn = UIButton()
        // 设置btn.
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.sizeToFit()
        
        // 给按钮添加点击事件.
        btn.addTarget(self, action: Selector("StartBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    // MARK: 开始按钮缩放动画.
    private func startBtnAnimation () {
        
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
//              TJPrint("新特性界面O了")
        }
    }
    
}

// MARK - scrollView的代理方法.
extension TJNewFeatureViewController {

    // 减速完成是后调用.
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        // 计算页数.
        let pageIndex = Int ((scrollView.contentOffset.x + 0.5 * UIScreen.mainScreen().bounds.width) / UIScreen.mainScreen().bounds.width)
//        TJPrint(pageIndex)
        if pageIndex == NumberOfNewFeatureCount - 1 {
            let inexPath = NSIndexPath(forItem: pageIndex, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(inexPath) as! TJCollectionViewCell
            // 开始执行开始按钮动画.
            cell.startBtnAnimation()
        }
    }
    
}




